#include<iostream>
#include<cerrno>
#include<cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include "comm.h"
//负责读取

bool Makefifo()
{
    int n = mkfifo(FILENAME,0666);
    if(n<0)
    {
        std::cerr<<"errno: "<<errno<<", errrstring :"<<strerror(errno)<<std::endl;
        
        return false;
    }
    std::cout<<"mkfifo success!"<<std::endl;
    return true;
}
int main()
{
Start:
    int rfd=open(FILENAME,O_RDONLY);
    if(rfd<0)
    {
        std::cerr<<"errno: "<<errno<<", errrstring :"<<strerror(errno)<<std::endl;  
        if(Makefifo()) goto Start;
        else return 1;
    }
    std::cout<<"open read success!"<<std::endl;

    char buff[MAX];
    while(true)
    {
        ssize_t n=read(rfd,buff,sizeof(buff)-1);
        if(n >0)
        {
            buff[n] = 0;
            std::cout<<"client say: "<<buff<<std::endl;
        }
        //读取到结尾
        else if(n==0)
        {
            std::cout<<"I will exit"<<std::endl;
            break;
        }
    }
    close(rfd);
    std::cout<<"close success!"<<std::endl; 
    return 0;
}


